<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/6/9
 * Time: 18:16
 */

namespace App\Http\Controllers\Service;
use App\Http\Model\MemberModel;
use App\Http\Model\TempPhone;
use App\Tools\SMS\SendTemplateSMS;
use Code;
use App\Http\Controllers\CommonController;
use Illuminate\Support\Facades\Log;
use Symfony\Component\HttpFoundation\Request;

class LoginController extends CommonController {
    public function login() {
        return view('login');
    }

    public function register(Request $request) {
        if($request->isMethod("POST")){
            $phone = $request->input('phone','');
            $password = $request->input('password','123456');
            $phone_code = $request->input('phone_code','');

            $tp = new TempPhone();
            $info =  $tp->getLastDataByPhone('15764222702');
            if($info->TpCode==$phone_code){
                if(time()<strtotime($info->TpDeadline)){
                    $member = new MemberModel();
                    if($member->addNewData($phone,'gds',md5('bk'.$password))){
                        exit(responseJson(0,"注册成功"));
                    }
                }else{
                    exit(responseJson(0,'手机验证码不正确超时'));
                }
            }else{
                exit(responseJson(0,'手机验证码不正确'));
            }
        }
        return view('register');
    }

    public function verifyCode() {
        $code = new Code();
        $code->make();
    }

    public function sendSMS(Request $request) {
        $phoneNum = $request->input('phone');
        $code = createRandomNum(6);
        $sms  = new SendTemplateSMS;
        $res  = $sms->sendTemplateSMS($phoneNum,array($code,2),1);
        if($res->status == 0){
            $tempPhone = new TempPhone();
            if(!$tempPhone->addNewData($phoneNum,$code)){
                $res->message='插入数据库失败';
                return $res->toJson();
            }
            $res->message = "发送成功";
            return $res->toJson();
        }else{
            $res->message = "发送失败";
            return $res->toJson();
        }
    }


    public function test() {

    }
}